<?php
if($_GET['sidebarSettingsPage']) {
	if(is_callable($_GET['sidebarSettingsPage']))
		$output = $admin->ob_handle_cust_func($_GET['sidebarSettingsPage']);
	echo $output;
	exit();
}
global $pluginData;
foreach($pluginData as $key => $value) {
	if(array_key_exists('sidebar', $value)) {
		foreach($value['sidebar'] as $sidebarItem) {
			$sidebarBlocks[$sidebarItem['callFunction']] = $value['name'];
		}
	}
}
if($_POST['payload']) {
	$payload = $_POST['payload'];
	$payload = unserialize(urldecode(stripslashes($payload)));
	$sql = "DELETE FROM ".DBTABLEPREFIX."sidebar";
	$query = $admin->db_query($sql);
	foreach($payload as $key => $value) {
		$pluginTitle = $sidebarBlocks[$value['func']];
		$sql = "INSERT into ".DBTABLEPREFIX."sidebar (name, plugin, data, func) VALUES ('{$value['title']}', '{$pluginTitle}', '', '{$value['func']}')";
		$query = $admin->db_query($sql) or die('asdf');
	}
	echo $admin->message('success', 'Sidebar Saved', 'You can see your changes applied!');
	exit();
}
plugin('admin-top-sidebar');

$sidebarItems = $pluginData;
foreach($sidebarItems as $key => $value) {
	if(array_key_exists('sidebar', $value)) {
		foreach($value['sidebar'] as $sidebarItem) {
			if(in_array($value['name'], $admin->activePlugins))
				$sidebar[$sidebarItem['title']] = $sidebarItem['callFunction'];
		}
	}
}
$sidebarItemFormat = <<<EOT
	<li name='%2\$s' id="sideblock-%2\$s"><a href="javascript:;" class="deleteSidebar"><img src="includes/admin/images/icons/delete-16x16.png" /></a><a href="javascript:;" class="editSidebar">rename</a><span class="title">%1\$s</span></li>
EOT;
foreach($sidebar as $key => $value) {
	$lis .= sprintf($sidebarItemFormat, $key, $value);
}
$currSide = $admin->getSidebar();
foreach((array) $currSide as $key => $value) {
	$list .= sprintf($sidebarItemFormat, $key, $value['func']);
}

$output = <<<EOT
<script type="text/javascript" src="includes/admin/js/jquery-ui-sortable-1.6rc4.packed.js"></script>
<script type="text/javascript">
	$(document).ready(function () {
		$("#dropZone").sortable({
			revert : true,
			axis: 'y',
			delay : 100
		});
		$("#dragList li").draggable({
			connectToSortable:'#dropZone',
			helper:'clone',
			revert:'invalid',
			delay : 100
		});
		$('a.deleteSidebar').live('click', function () {
			var parent = $(this).parent();
			$(parent).remove();
			return false;
		});
		$('#saveSidebar').click(function () {
			var newArr = new Array();
			$('#dropZone li').each(function (i, domEle) {
				newArr.push({
					'title' : Trim($(domEle).find('.title').text()),
					'func' : $(domEle).attr('name')
				});
			});
			var result = serialize(newArr);
			$('.response').html('');
			$.post('admin.php?page=sidebar&save=true', 'payload='+result, function (data) {
				$('.response').html(data);
				addClose();
			});
		});
		$('.editSidebar').live('click', function () {
			if($(this).text() == 'save') {
				$(this).parent().find('input').blur();
			}
			else {
				var editBox = '<input type="text" style="width:70%;" value="' + $(this).parent().find('.title').html() + '" />';
				var insert = $(this).after(editBox);
				$(this).parent().find('span').hide().parent().find('.editSidebar').html('save').parent().parent().addClass('hover');
				var parent = $(this).parent().find('input');
				$(parent).select().keypress(function(e) {
					if(e.which == 13) {
					  var inputVal = $(this).val();
					  var asdf = $(this).parent().find('.title');
					  $(asdf).html(inputVal).show().parent().find('.editSidebar').html('rename');
					  $(this).remove();
					}
				});
				$(parent).blur(function() {
					  var inputVal = $(this).val();
					  var asdf = $(this).parent().find('.title');
					  $(asdf).html(inputVal).show().parent().find('.editSidebar').html('rename');
					  $(this).remove();
				});
			}
		});
		$('#dropZone li').live('mouseover', function () {
			$(this).addClass('hover');
		}).live('mouseout', function () {
			$(this).removeClass('hover');
		});
	});
</script>
<div class='response'>&nbsp;</div>
<div id="sidebarDrop">
	<div class="pad">
		<h2>Sidebar</h2>
		<ul id="dropZone" class="connect">
			{$list}
		</ul>
		<p class="sidebarhelp">
			<button id="saveSidebar">Save Sidebar</button>
		</p>
	</div>
</div>
EOT;
$output .= <<<EOT
<div id="dragListWrap">
	<h2>Avaliable Sidebar Items</h2>
	<div class="dragContent">
	<form action="" method="post" id="sidebarItemSettings">
		<fieldset>
			
		</fieldset>
	</form>
	<ul id='dragList' class="connect">
		{$lis}
		<li name='navigation' id="sideblock-navigation"><a href="" class="deleteSidebar"><img src="includes/admin/images/icons/delete-16x16.png" /></a><a href="javascript:;" class="editSidebar">rename</a><span class="title">Navigation</span></li>
		<li name='secondary_navigation' id="sideblock-secondary_navigation"><a href="" class="deleteSidebar"><img src="includes/admin/images/icons/delete-16x16.png" /></a><a href="javascript:;" class="editSidebar">rename</a><span class="title">Secondary Navigation</span></li>
	</ul>
<br class="clear"/>
	<p class="dragListHelp">
		Drag things from over here to the left side.
	</p>
	</div>
</div>
EOT;
$admin->page_info['content']['page_title'] = "Sidebar";
$admin->page_info['content']['page_content']	= $output;
plugin('admin-bottom-sidebar');
//Display page
include(BASE_URL.'includes/admin/admin.php');

?>